// pages/appointment-teacher/index.js
const handler = require("../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    spinShow: true,
    visible1: false,
    actions1: [
      {
        name: '预约课程',
      },
      {
        name: '去分享',
        icon: 'share',
        openType: 'share'
      }
    ],
    scrollTop: false,
    title_active: true,
    left: 0,

    page_country: 1,//国家级导师当前页数
    page_province: 1,//省级导师当前页数
    current_user_type: 0,//用户类型 0、国家级导师 1、省级导师
    tearcher_data: [],//导师数据
    loading1:false,
    loading2:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.get_data();
    this.initData();
  },
  initData: function () {
    var para = {
      page_country: this.data.page_country,
      page_province: this.data.page_province,
    };    
    var that = this;
    if(that.data.current_user_type == 0){
       that.setData({
         loading1:true
       })
    }else{
      that.setData({
        loading2: true
      })
    }
    handler.requestData("index", para, function (res) {
      if (res.success == true) {
        
        that.setData({
          spinShow: false,
          tearcher_data: res.data,
          country_data: res.data.country_data.data,
          province_data: res.data.province_data.data
        })
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        if (that.data.current_user_type == 0) {
          that.setData({
            loading1: false
          })
        } else {
          that.setData({
            loading2: false
          })
        }
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
      // console.log(that.data.page_province);
      // console.log(that.data.tearcher_data)
    });
  },
  // 页面跳转
  handler_next_page(e) {
    handler.intoPageHandler(e.currentTarget.dataset.url);
  },
   
  handleOpen1(e) {
    var user_id = e.currentTarget.dataset.userid;
    this.setData({
      visible1: true,
      tearchid: user_id
    });
  },
  handleCancel1() {
    this.setData({
      visible1: false
    });
  },
  onShareAppMessage() {
    return {
      title: 'iView Weapp',
      imageUrl: 'https://file.iviewui.com/iview-weapp-logo.png'
    };
  },
  handleClickItem1({ detail }) {
    handler.intoPageHandler("../teacher-detail/index?tearchid=" + this.data.tearchid);
  },
  onChange(event) {
    console.log(event.detail, 'click right menu callback data')
  },
  //页面滚动执行方式
  onPageScroll(event) {
    event.scrollTop > 164 && this.setData({ scrollTop: true })
    event.scrollTop < 164 && this.setData({ scrollTop: false })
  },
  // choose_teacher 选择教师
  choose_teacher: function (e) {
    var current_btn = e.currentTarget.dataset.type;
    const that=this;
    var { title_active, left } = this.data;
    if (current_btn === "county") {
      this.setData({
        title_active: true, left: 0, current_user_type: 0, 
          spinShow: false,
          country_data: that.data.tearcher_data.country_data.data,
          province_data: []
        });
      return;
    } else {
      this.setData({
        title_active: false, left: 750, current_user_type: 1, country_data: [],
        province_data: that.data.tearcher_data.province_data.data });
      return;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //下拉刷新
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉加载更多
    var page_province = this.data.page_province;
    var page_country = this.data.page_country;
    var current_user_type = this.data.current_user_type;
    if (current_user_type == 1) {
      var page_province_total = this.data.tearcher_data.province_data.total;
   
      if (page_province_total < this.data.page_province * 10) {
        console.log("已经是全部数据");
      } else {
        this.setData({
          page_province: page_province + 1,
          // loading1:true
        })
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.initData();
      }
    } else {

      var page_country_total = this.data.tearcher_data.country_data.total;
      if (page_country_total < this.data.page_country * 10) {
        console.log("已经是全部数据");
      } else {
        this.setData({
          page_country: page_country + 1,
          // loading2:true
        })
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.initData();
      }
     
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})